package indiwood.model.service;

import indiwood.model.CinemaVO;
import indiwood.model.DirectorListVO;
import indiwood.model.DirectorVO;
import indiwood.model.MovieVO;
import indiwood.model.PagingBean;
import indiwood.model.ShowingListVO;
import indiwood.model.ShowingVO;
import indiwood.model.dao.MovieDAO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
@Service
public class MovieServiceImpl implements MovieService {
	@Resource(name = "movieDAOImpl")
	private MovieDAO movieDAO;
	@Override
	public DirectorListVO directorList(String pageNo) {
		if(pageNo==null||pageNo=="") 
			pageNo="1";
		List<DirectorVO> dlist=movieDAO.getDirectorList(pageNo);
		int total=movieDAO.directorTotalContent();
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		DirectorListVO lvo=new DirectorListVO(dlist, paging);
		return lvo;
	}
	@Override
	public HashMap<String, Object> directorContent(int no) {
		HashMap<String, Object> directorDetailMap = new HashMap<String, Object>();
		DirectorVO dvo = movieDAO.getDirectorContent(no);
		directorDetailMap.put("directorContent", dvo);
		List<MovieVO> mlist = movieDAO.getMovieList(dvo.getDirectorNo());
		directorDetailMap.put("movielist", mlist);
		String awardstr = "";
		for(int i=0; i<mlist.size();i++){ // 영화 리스트를 반복문을 돌려 하나씩 확인
			List<String> alist = movieDAO.getAwardList(mlist.get(i).getMovieNo()); // 영화번호에 해당되는 수상리스트를 검색
			for(int j=0;j<alist.size();j++){ // 수상리스트를 반복문을 돌려 하나씩 확인
				if(awardstr.indexOf(alist.get(j))==-1){ // awardstr 에 해당 수상정보가 없으면
					awardstr += alist.get(j); // awardstr 에 수상정보 추가
					if(j<alist.size()-1){
						awardstr += ", ";
					}
				}
			}
		}
		directorDetailMap.put("awardlist", awardstr);
		return directorDetailMap;
	}
	@Override
	public CinemaVO defaultCinema() {
		return movieDAO.defaultCinema();
	}
	@Override
	public CinemaVO cinemaContent(int cinemaNo) {
		return movieDAO.cinemaContent(cinemaNo);
	}
	@Override
	public HashMap<String, Object> cinemaListBylatlng(CinemaVO cvo) {
		HashMap<String, Object> cinemaMap = new HashMap<String, Object>();
		CinemaVO cvoSelected = movieDAO.cinemaListBylatlng(cvo);
		List<CinemaVO> cinemaList = movieDAO.cinemaList();
		cinemaMap.put("cinemaVO", cvoSelected);
		cinemaMap.put("cinemaList", cinemaList);
		return cinemaMap;
	}
	@Override
	public ShowingListVO getSoonList(String pageNo) {
		if(pageNo==null||pageNo=="")
			pageNo="1";
		List<ShowingVO> slist=movieDAO.getSoonListByPage(pageNo);
		int total=movieDAO.soonListTotalContent();
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		paging.setNumberOfContentPerPage(8);
		ShowingListVO svo=new ShowingListVO(slist, paging);
		return svo;
	}
	@Override
	public ShowingListVO getShowingList(String pageNo) {
		if(pageNo==null||pageNo=="")
			pageNo="1";
		List<ShowingVO> slist=movieDAO.getShowingListByPage(pageNo);
		int total=movieDAO.showingListTotalContent();
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		paging.setNumberOfContentPerPage(8);
		ShowingListVO svo=new ShowingListVO(slist, paging);
		return svo;
	}
	@Override
	public Map<String, Object> movieContent(int movieNo) {
		HashMap<String, Object> mcMap = new HashMap<String, Object>();
		mcMap.put("movieContent", movieDAO.movieContent(movieNo));
		String awardContent = "";
		List<String> alist = movieDAO.getAwardList(movieNo); // 영화번호에 해당되는 수상리스트를 검색
		for(int j=0;j<alist.size();j++){ // 수상리스트를 반복문을 돌려 하나씩 확인
			if(awardContent.indexOf(alist.get(j))==-1){ // awardstr 에 해당 수상정보가 없으면
				awardContent += alist.get(j); // awardstr 에 수상정보 추가
				if(j<alist.size()-1){
					awardContent += ", ";
				}
			}
		}
		mcMap.put("awardContent", awardContent);// 수상 정보 추가
		return mcMap;
	}
}
